<?php

class MesController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
         // action body
    	$frm = new Application_Form_Mes();
    	$this->view->form = $frm;
    }
    
    public function listAction()
    {
        $frm = new Application_Form_Mes();
        $this->view->form = $frm;
    }

    public function detailsAction()
    {
    	$this->_helper->layout()->disableLayout();
    	
    	$mdl = new Application_Model_Dia ();
    	$mdlValor = new Application_Model_DiaValorTotal ();
    	
    	$mdlConta = new Application_Model_Conta();
    	
    	$dia_selecionado = $this->getRequest()->getParam('data');
    	$dia_dividido = explode('-',$dia_selecionado);
    	
    	$days = $this->_helper->DiaUtil($dia_dividido[0], $dia_dividido[1]);
    	$days_next = $this->_helper->DiaUtil(($dia_dividido[0])+1, $dia_dividido[1]);
    	
    	
    	$id_dia_1 = $mdl->selectDay($days);
    	$id_dia_2 = $mdl->selectDay($days_next);
    	
    	$contas_periodo = $mdlConta->selectBetween($id_dia_1['id_dia'], $id_dia_2['id_dia']);
    	
    	
    	$this->view->contas = $contas_periodo;
    	
    	$date2banco = $this->_helper->Data2Banco($days);
    	$diferenca = $this->_helper->DiaIntervalo($days,$days_next);
    	
    	$ts = strtotime ( $date2banco );
		
    	$day 	= date	( 'd', $ts );
    	$month 	= date	( 'm', $ts );
		$year 	= date	( 'Y', $ts );
		
		$dias_semana = array (	'DOM', 'SEG', 'TER', 'QUA', 'QUI', 'SEX', 'SAB' );
		
		$itens = array();
		for($i = 0; $i < $diferenca; $i ++) 
		{
			$timestamp = mktime ( 0, 0, 0, $month, ($day) + $i, $year );
			$id_dia = $mdl->selectDay ( (date ( 'Y-m-d', $timestamp )) );
			$valor = $mdlValor->selectDay ( (date ( 'Y-m-d', $timestamp )) );
			
			$itens[$i]['data']	= date('d-m-Y',$timestamp);
			$itens[$i]['id']	= $id_dia['id_dia'];
			$itens[$i]['sem']	= $dias_semana [date ( 'w', $timestamp )];
			$itens[$i]['valor']	= $valor['valor'];
			
		}
		
		$this->view->itens = $itens;
		if($month == date('m'))
		{
			$this->view->cash = $this->getRequest()->getParam('valor');
		}
		else 
		{
			$mdlPagamento = new Application_Model_Pagamento();
			$this->view->cash = $mdlPagamento->selectCurrentValue();
		}
    }


}





